import { Injectable } from '@angular/core';
import { Catalogue } from '../catalogues-model';
import { CATALOGUES } from '../catalogues';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/toPromise';

@Injectable()
// export class catalogueService {

//     constructor() { }

//     getCatalogues(): Catalogue[] {
//         return CATALOGUES;
//     }
// }

export class catalogueService {

    private catalogueurl = 'demo/src/biz/book-catalogue-component/model/catalogueService/catalogues';
    private headers = new Headers({ 'Content-Type': 'application/json' });
    private catalogue = new Catalogue();
    constructor(private http: Http) { }

    // getCatalogues(): Promise<Catalogue[]> {
    //     debugger;
    //     return this.http.get(this.catalogueurl)
    //         .toPromise()
    //         .then(response => response.json().data as Catalogue[])
    //         .catch(this.handleError);
    // }

    // getCatalogues():any {
    //     debugger;
    //     return this.http.get(this.catalogueurl)
    //         .toPromise()
    //         // .then(response => response.json().data as Catalogue[])
    //         // .catch(this.handleError);
    // }
    getCatalogues(): Promise<Catalogue[]> {
        return Promise.resolve(CATALOGUES);
    }

    getCatalogue(id: any): Promise<Catalogue> {
        return this.getCatalogues()
            .then(catalogues => catalogues.find(catalogue => catalogue.id === id));
    }

    update(catalogue: Catalogue): boolean {
        // CATALOGUES.push[JSON.parse(catalogue)];
        let pair = { key: catalogue.id, value: catalogue.name };
        let valid = this.validateName(catalogue.name);
        if (valid === 0) {
            let index = this.search(pair);
            CATALOGUES[index] = catalogue;

            return true;
        }
        return false;
    }

    add(id: any, name: string) {
        let pair = { key: id, value: name };
        // let validateId = this.validate(id);
        // if (validateId === -1 || validateId > 0) return;

        // let validateName = this.validate(name);
        // if (validateName === -1 || validateName > 0) return;

        let index = this.validate(pair);

        if (index < 0) return;
        if (index < CATALOGUES.length) return;
        let addcatalogue = new Catalogue();
        addcatalogue.id = id;
        addcatalogue.name = name;
        addcatalogue.booklists = {};

        CATALOGUES.push(addcatalogue);
        // CATALOGUES = test;
        // if (this.validate(id) || this.validate(name)) return;
        // else {
        //     // this.catalogue.id = id;
        //     // this.catalogue.name = name;
        //     // this.catalogue.booklists = {};

        //     // CATALOGUES[CATALOGUES.length] = this.catalogue;
        // }
        // else{
        //     alert("The catalogue exists already ")
        // }
    }

    delete(catalogue: Catalogue) {
        // let originlength = CATALOGUES.length;
        let pair = { key: catalogue.id, value: catalogue.name }
        let index = this.search(pair);
        CATALOGUES.splice(index, 1);
        // for (let i = index - 1; i < originlength - 1; ++i) {
        //     CATALOGUES[i].id = i + 1;
        // }
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }

    private search(pair: any): number {
        if (CATALOGUES.length === 0) return 0;
        else {

            for (let i = 0; i < CATALOGUES.length; ++i) {
                if (CATALOGUES[i].id === pair.key || CATALOGUES[i].name === pair.value) return i;
            }
            return CATALOGUES.length;
        }
    }

    private validateName(name: string) {
        if (name === undefined || name === null || name.trim().length === 0) {
            alert("Please input valid value");
            return -1;
        }
        return 0;
    }

    private validate(pair: any): number {
        if (pair.key === undefined || pair.key === null || pair.key.trim().length === 0) {
            alert("Please input valid value");
            return -1;
        }

        if (pair.value === undefined || pair.value === null || pair.value.trim().length === 0) {
            alert("Please input valid value");
            return -1;
        }

        let index = this.search(pair);
        if (index < CATALOGUES.length) {
            alert("id = " + pair.key + " or " + "name = " + pair.value + " exists already!");
        }

        return index;
    }
}